package com.sky.controller.user;

import com.sky.baidu.BaiDuUtil;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Api(tags = "C端-订单相关接口")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * 用户下单
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("用户下单：{}",ordersSubmitDTO);
        // 下订单
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    /**
     * 历史订单查询
     * @param ordersPageQueryDTO
     * @return
     */
    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public Result<PageResult> historyOrders(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("用户历史订单查询：{}",ordersPageQueryDTO);
        PageResult pageResult = orderService.historyOrders(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 订单详情查询
     * @param id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("订单详情查询")
    public Result<Object> orderDetail(@PathVariable Long id){
        log.info("订单详情查询：{}",id);
        OrderVO orderVO = orderService.getByIdOrderDetail(id);
        return Result.success(orderVO);
    }

    /**
     * 取消订单
     * @param id
     * @return
     */
    @PutMapping("cancel/{id}")
    @ApiOperation("取消订单")
    public Result<Object> cancel(@PathVariable Long id){
        log.info("取消订单：{}",id);
        orderService.cancel(id);
        return Result.success();
    }

    /**
     * 再来一单 将原订单中的商品重新加入到购物车中
     * @param id
     * @return
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result<Object> repetition(@PathVariable Long id){
        log.info("再来一单：{}",id);
        orderService.repetition(id);
        return Result.success();
    }

    /**
     * 支付成功
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("payment")
    @ApiOperation("支付成功")
    public Result<Object> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO){
        log.info("支付成功：{}",ordersPaymentDTO);
        Orders payment = orderService.payment(ordersPaymentDTO);
        return Result.success(payment);
    }

    @GetMapping("reminder/{id}")
    @ApiOperation("催单")
    public Result<Object> reminder(@PathVariable Long id){
        log.info("催单：{}",id);
        orderService.reminder(id);
        return Result.success();
    }

}
